import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.Supplier;

/**
 * @Author DengJia
 * @Date 2021/8/5 23:50
 * @Description
 */

public class MyPracticeDemo {
    public static void main(String[] args) {
//        p1();
//        p2();
        // while(true)
        // p3();
        long l1 = System.currentTimeMillis();
        p4_1(100000);
        long l2 = System.currentTimeMillis();
        p4_2(100000);
        long l3 = System.currentTimeMillis();

        System.out.println("p4_1：" + (l2 - l1) + "（ms）");
        System.out.println("p4_2：" + (l3 - l2) + "（ms）");

    }

    /**
     * 两个for循环 + %取余来打印素数
     */
    public static void p4_1(int k) {
        for (int i = 2; i < k; i++) {
            // 素数只被自身和1整除
            for (int j = 2; j <= i; j++) {
                if (i % j == 0 && i != j) break;
                if (j == i) System.out.print(j + " ");
            }
        }
        System.out.println();
    }

    public static void p4_2(int k) {
        for (int i = 2; i < k; i++) {
            int flag = 1;
            for (int j = 2; j <= i / 2; j++) {
                if (i % j == 0) {
                    flag = 0;
                    break;
                }
            }
            if (flag == 1) {
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }

    /**
     * 打印 1-100 的值
     */
    public static void p1() {
//        Stream<Integer> stream = Stream.generate(new MySupplier());
//        System.out.println("1、打印从1到100的值：\n" + stream.limit(100).collect(Collectors.toList()) + "\n");
        for (int i = 0; i < 100; i++) {
            System.out.print(i + 1 + " ");
        }
        System.out.println();
    }

    /**
     * 产生25个int随机数
     * 使用if-else语句来将其分类（大于、小于、等于紧跟它而随机生成的值）
     */
    public static void p2() {
        Random random = new Random();
        int[] ints = new int[25];
        List<Integer> list = new ArrayList<>();
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < 25; i++) {
            int pre, next;
            ints[i] = random.nextInt(100);
            pre = i == 0 ? ints[i] : ints[i - 1];
            next = ints[i];

            if (i == 0) {
                builder.append(pre);
            } else {
                if (pre < next) {
                    builder.append(" < ").append(next);
                } else if (pre > next) {
                    builder.append(" > ").append(next);
                } else {
                    builder.append(" = ").append(next);
                }
            }
        }
        System.out.println(Arrays.toString(ints));
        System.out.println(builder);
    }

    /**
     * 参考p3改写为while无限循环
     */
    public static void p3() {
        List<Integer> list = new ArrayList<>();
        Random random = new Random();
        StringBuilder builder = new StringBuilder();
        int k = 0;
        int pre, next;
        while (true) {
            list.add(random.nextInt(100));
            pre = k == 0 ? list.get(k) : list.get(k - 1);
            next = list.get(k);
            if (k == 0) {
                System.out.print(pre);
//                builder.append(pre);
            } else {
                if (pre > next) {
                    System.out.print(" > " + next);
//                    builder.append(" > ").append(next);
                } else if (pre < next) {
                    System.out.print(" < " + next);
//                    builder.append(" < ").append(next);
                } else {
                    System.out.print(" = " + next);
//                    builder.append(" = ").append(next);
                }
            }
            k++;
        }
    }
}

class MySupplier implements Supplier<Integer> {
    int i = 0;

    @Override
    public Integer get() {
        return ++i;
    }
}
